package com.chat.demo.userservice;

import com.chat.demo.userservice.DTO.message.MessageDTO;
import com.chat.demo.userservice.DTO.message.PrivateMessageDTO;
import com.chat.demo.userservice.Entity.DAO.MessageDao;
import com.chat.demo.userservice.Entity.DAO.MessageMapper;
import com.chat.demo.userservice.Entity.Entity.Message;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.util.List;

// 私聊 Service 实现
@Service
public class Message_Service {

    private final MessageDao messageDao;
    private final WebSocketSender webSocketSender;
    private final MessageMapper messageMapper;
    private final User_Service userService;

    public Message_Service(MessageDao messageDao,
                           WebSocketSender webSocketSender,
                           MessageMapper messageMapper,
                           User_Service userService) {
        this.messageDao = messageDao;
        this.webSocketSender = webSocketSender;
        this.messageMapper = messageMapper;
        this.userService = userService;
    }

    public void sendMessage(Long senderId, Long receiverId, String content) {
        MessageDTO message = new MessageDTO();
        message.setSenderId(senderId);
        message.setReceiverType("user");
        message.setReceiverId(receiverId);
        message.setContent(content);
        message.setStatus("sent");

        messageDao.sendMessage(message);

        // 构造推送消息
        PrivateMessageDTO chatMessage = new PrivateMessageDTO();
        chatMessage.setContent(content);
        chatMessage.setSenderId(senderId);
        String name = userService.getUserById(senderId).getUsername();
        chatMessage.setSenderName(name);
        chatMessage.setReceiverId(receiverId);
        chatMessage.setType("PRIVATE");
        chatMessage.setTimestamp(OffsetDateTime.now());
        chatMessage.setStatus("sent");

        // 推送给接收人
        webSocketSender.sendMessageToUser(receiverId, chatMessage);
    }

    public void updateMessageStatus(Long messageId, String status) {
        messageDao.updateMessageStatus(messageId, status);
    }

    public List<MessageDTO> getChatHistory(Long userId1, Long userId2) {
        return messageDao.getPrivateChatHistory(userId1, userId2);
    }

    // 发送群组消息
    /*public boolean sendGroupMessage(int senderId, int roomId, String content) {
        try {
            // 存储消息到数据库
            messageMapper.sendRoomMessage(senderId, roomId, content);

            // 构造 ChatMessage 对象
            GroupMessage chatMessage = new GroupMessage();
            chatMessage.setContent(content);
            chatMessage.setSenderId((long) senderId);
            chatMessage.setType("GROUP");
            chatMessage.setGroupId((long) roomId);

            // 广播给所有订阅了 /topic/group.{roomId} 的用户
            webSocketSender.broadcastMessageToGroup(roomId, chatMessage);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }*/

    // 获取群组聊天历史记录
    public List<Message> getGroupHistory(int roomId, int page, int size) {
        int offset = (page - 1) * size;
        return messageMapper.getRoomMessages(roomId, size, offset);
    }

    // 获取群组所有成员（可选：用于权限控制）
    public List<Integer> getGroupMembers(int roomId) {
        return messageMapper.getRoomMembers(roomId);
    }
}